<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="学期学年:">
        <el-select class="selectWidth" v-model="dataForm.studyId" placeholder="学期学年" >
          <el-option
            v-for="item in studyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程名称:">
        <el-input class="selectWidth" v-model="dataForm.name" placeholder="课程名称" clearable></el-input>
      </el-form-item>
      <el-form-item  label="课程类型:">
        <el-select class="selectWidth" v-model="dataForm.type" placeholder="课程类型" >
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="exportDataList()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="tableRef"
      :data="dataList" :height="height"
      :header-cell-style="tableHeaderStyle"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="课程名称">
      </el-table-column>
      <el-table-column
        prop="gmcou"
        header-align="center"
        align="center"
        width="120"
        label="课程购买人数">
      </el-table-column>
      <el-table-column
        prop="xxcou"
        header-align="center"
        align="center"
        width="120"
        label="参加学习人数">
      </el-table-column>
      <el-table-column
        prop="xxwccou"
        header-align="center"
        align="center"
        width="120"
        label="学习完成人数">
      </el-table-column>
      <el-table-column
        prop="pjcou"
        header-align="center"
        align="center"
        width="120"
        label="参与评价人数">
      </el-table-column>
      <el-table-column
        prop="kscou"
        header-align="center"
        align="center"
        width="120"
        label="参加考试人数">
      </el-table-column>

      <el-table-column
        prop="kstgcou"
        header-align="center"
        align="center"
        width="120"
        label="考试通过人数">
      </el-table-column>


      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="addOrUpdateHandle(scope.row)">详情</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <StatDetail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></StatDetail>
  </div>
</template>

<script>
import StatDetail from './state-detail.vue'
import { saveAs } from 'file-saver';

  export default {
    data () {
      return {
        tableHeaderStyle: { background: '#f5f7fa !important', height: '40px !important', padding: '4px !important' },

        studyOptions:[

        ],
        typeOptions:[
          {
            value: '',
            label: '请选择'
          },{
            value: 0,
            label: '专业科目'
          }, {
            value: 1,
            label: '公需科目'
          }
        ],
        height: 0,
        dataForm: {
          name: '',
          studyId: '',
          type: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      StatDetail
    },
    activated () {
      this.getstudyOptionsList()

      this.getDataList()
    },
    mounted() {
      console.log(document.body.clientHeight)
      // 至于减去多少，这个要看你的页面高度和你想将滚动轴固定在哪里了，数值可以更换
      this.$nextTick(() => {
        let tableHeight = document.body.clientHeight - 275
        this.height = tableHeight
      })
    },
    methods: {
      repairTable() {
        this.$nextTick(() => {
          this.$refs.tableRef.doLayout()
        })
      },
      //获取学期学年数据
      getstudyOptionsList () {
        this.$http({
          url: this.$http.adornUrl('/class/study/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 9999999
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            if (data.page.list&&data.page.list.length>0){
              for (let i = 0; i < data.page.list.length; i++) {
                this.studyOptions.push({
                  value: data.page.list[i].id,
                  label: data.page.list[i].name
                })
              }
              //默认选中第一个
              this.dataForm.studyId = this.studyOptions[0].value
            }
          }
        })
      },
      exportDataList () {
        let searchParams = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.name,
          'studyId': this.dataForm.studyId
        }

        this.$http.exportFile('/class/manage/exportStatList',  searchParams)
          .then(async (res)  => {
            console.log(res);
            let blob = new Blob([res])
            saveAs(blob, "课程统计.xlsx")
          }).catch(error => { console.error('文件导出失败:', error) })
        // this.$http({
        //   url: this.$http.adornUrl('/class/manage/exportStatList'),
        //   method: 'post',
        //   params: this.$http.adornParams(searchParams),
        // }).then(async (res) => {
        //     console.log(res);
        //     let blob = new Blob([res.data], { type: 'application/zip' })
        //   console.log(blob);
        //     saveAs(blob, "人信息.xlsx")
        //
        // }).catch((r) => {
        //   console.error(r)
        // })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let searchParams = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.name,
          'studyId': this.dataForm.studyId,
          'type': this.dataForm.type
        }
        this.$http({
          url: this.$http.adornUrl('/class/manage/statList'),
          method: 'get',
          params: this.$http.adornParams(searchParams)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
          this.repairTable()

        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        console.log(val)
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (item) {
        this.addOrUpdateVisible = true
        //获取当前选中的学年学期
        this.studyOptions.forEach((value, index) => {
          if (value.value == this.dataForm.studyId) {
            item.studyName = value.label
          }
        })
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(item)
        })
      },
    }
  }
</script>
